package com.farmpost.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farmpost.server.mapper.UserBindMapper;
import com.farmpost.server.pojo.RespBean;
import com.farmpost.server.pojo.UserBind;
import com.farmpost.server.pojo.UserInfo;
import com.farmpost.server.service.IUserBindService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户绑定表 服务实现类
 * </p>
 *
 * @author king
 * @since 2022-04-03
 */

@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements IUserBindService {

    @Autowired
    private UserBindMapper userBindMapper;
    /**
     * 用户绑定信息插入
     * @param userBind
     * @return
     */

    @Override
    public RespBean insertUserBind(UserBind userBind) {
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (userBind.getUserId() == null)
            userBind.setUserId(user.getUserId());
        int insert = userBindMapper.insert(userBind);
        if (insert == 1)
            return RespBean.success("用户绑定成功！");
        return RespBean.error("用户绑定失败");
    }

    /**
     * 用户绑定信息修改
     * @param userBind
     * @return
     */
    @Override
    public RespBean updateUserBind(UserBind userBind) {
        System.out.println(userBind);
        UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        int update = userBindMapper.update(userBind, new UpdateWrapper<UserBind>().eq("user_id", user.getUserId()));
        if (update == 1)
            return RespBean.success("用户绑定信息修改成功");
        return RespBean.error("用户绑定信息修改失败");
    }

    /**
     * 用户绑定信息查询
     * @param userId
     * @return
     */

    @Override
    public UserBind getUserBind(Integer userId) {
        return userBindMapper.selectOne(new QueryWrapper<UserBind>().eq("user_id", userId));
    }
}
